
//
// Copyright (c) 2008, the /*jupiter jazz*/ group
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the /*jupiter jazz*/ group nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY the /*jupiter jazz*/ group ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL the /*jupiter jazz*/ group BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

// Interface header.
#include "commandline.h"

// Standard headers.
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

//
// CommandLine class implementation.
//

// Constructor.
CommandLine::CommandLine()
{
    // Help.
    m_help.add_name("--help");
    m_help.add_name("-h");
    m_help.set_description("print program usage and exit");
    m_parser.add_option_handler(&m_help);

    // Progress messages.
    m_progress_messages.add_name("--progress");
    m_progress_messages.add_name("-p");
    m_progress_messages.set_description("print progress messages");
    m_parser.add_option_handler(&m_progress_messages);

    // Filenames.
    m_filenames.set_min_value_count(2);
    m_filenames.set_max_value_count(2);
    m_parser.set_default_option_handler(&m_filenames);

    // Tile size.
    m_tile_size.add_name("--tile-size");
    m_tile_size.add_name("-t");
    m_tile_size.set_description("set the size of the tiles in the output image");
    m_tile_size.set_syntax("width height");
    m_tile_size.set_min_value_count(2);
    m_tile_size.set_max_value_count(2);
    m_parser.add_option_handler(&m_tile_size);
}

// Parse program command line.
void CommandLine::parse(const int argc, const char* argv[])
{
    // Parse the command line.
    m_parser.parse(argc, argv);

    // Print program usage.
    if (m_help.found() || !m_filenames.found())
        print_program_usage(argv[0]);

    // Exit if necessary.
    if (m_help.found() || !m_filenames.found())
        exit(0);

    // Print messages generated during command line parsing.
    m_parser.print_messages();
}

// Print program usage.
void CommandLine::print_program_usage(const char* program_name) const
{
    cerr << "copyright (c) 2008, the /*jupiter jazz*/ group" << endl;
    cerr << "usage: " << program_name << " [options] input output" << endl;
    cerr << "options:" << endl;
    m_parser.print_usage();
}
